﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class Combo_N : FormBase1
    {
        public delegate void DoAction(string classcode);//如果有参数的话，定义参数 

        public DoAction FatherRefresh;
        int count = 0;
        string ClassCode = "";
        public Combo_N(string _ClassCode)
        {
            InitializeComponent();
            ClassCode = _ClassCode;
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            valildbefore();
            if (dxValidationProvider1.Validate())
            {
                SVC.client.Combo c = new SVC.client.Combo();
                if (lue_Carrier.EditValue!=null)
                {
                    c.CarrierID = int.Parse(lue_Carrier.EditValue.ToString());
                }
                c.ClassCode=lue_Class.EditValue.ToString();
                c.ComboCode = te_Code.Text;
                c.ComboName = te_name.Text;
                c.DataSize = te_datasize.Text;
                c.inprice = decimal.Parse(sp_inprice.EditValue.ToString());
                c.DealerPrice1 = decimal.Parse(sp_DealerPrice1.EditValue.ToString());
                c.DealerPrice2 = decimal.Parse(sp_DealerPrice2.EditValue.ToString());
                c.DealerPrice3 = decimal.Parse(sp_DealerPrice3.EditValue.ToString());
                c.DealerPrice4 = decimal.Parse(sp_DealerPrice4.EditValue.ToString());
                c.DealerPrice5 = decimal.Parse(sp_DealerPrice5.EditValue.ToString());
                c.Hours=te_hours.Text;
                c.LowPrice = decimal.Parse(sp_low.EditValue.ToString());
                c.monthFlag = cke_monthflag.Checked;
                c.outprice = decimal.Parse(sp_out.EditValue.ToString());
                c.Region = te_region.Text;
                c.Remarks = memoEdit1.Text;
                c.TCZ = int.Parse(sp_tcz.EditValue.ToString());
                
                string result = my.bw.Combo_Add(c, my.ACID);
                if(string.IsNullOrWhiteSpace(result))
                {
                    reset();
                    count++;
                }
                else
                {
                    Comm.ShowErorr(result);
                }
            }
        }

        private void valildbefore()
        {

        }
        private void reset()
        {
            te_Code.Text = "";
            te_datasize.Text = "";
            te_hours.Text = "";
            te_name.Text = "";
            te_region.Text = "";
            cke_monthflag.Checked = false;
            sp_inprice.EditValue = null;
            sp_DealerPrice1.EditValue = null;
            sp_DealerPrice2.EditValue = null;
            sp_DealerPrice3.EditValue = null;
            sp_DealerPrice4.EditValue = null;
            sp_DealerPrice5.EditValue = null;
            sp_low.EditValue = null;
            sp_out.EditValue = null;
            sp_tcz.EditValue = null;
            memoEdit1.Text = "";


        }

        private void btn_nowclose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            valildbefore();
            if (dxValidationProvider1.Validate())
            {
                SVC.client.Combo c = new SVC.client.Combo();
                if (lue_Carrier.EditValue != null)
                {
                    c.CarrierID = int.Parse(lue_Carrier.EditValue.ToString());
                }
                c.ClassCode = lue_Class.EditValue.ToString();
                c.ComboCode = te_Code.Text;
                c.ComboName = te_name.Text;
                c.DataSize = te_datasize.Text;
                c.inprice = int.Parse(sp_inprice.EditValue.ToString());
                c.DealerPrice1 = int.Parse(sp_DealerPrice1.EditValue.ToString());
                c.DealerPrice2 = int.Parse(sp_DealerPrice2.EditValue.ToString());
                c.DealerPrice3 = int.Parse(sp_DealerPrice3.EditValue.ToString());
                c.DealerPrice4 = int.Parse(sp_DealerPrice4.EditValue.ToString());
                c.DealerPrice5 = int.Parse(sp_DealerPrice5.EditValue.ToString());
                c.Hours = te_hours.Text;
                c.LowPrice = int.Parse(sp_low.EditValue.ToString());
                c.monthFlag = cke_monthflag.Checked;
                c.outprice = int.Parse(sp_out.EditValue.ToString());
                c.Region = te_region.Text;
                c.Remarks = memoEdit1.Text;
                c.TCZ = int.Parse(sp_tcz.EditValue.ToString());
                string result = my.bw.Combo_Add(c, my.ACID);
                if (string.IsNullOrWhiteSpace(result))
                {
                   
                    this.Close();
                }
                else
                {
                    Comm.ShowErorr(result);
                }
            }
        }

        private void btn_NewClass_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void Combo_N_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (count>0)
            {
                 FatherRefresh(lue_Class.EditValue.ToString());
            }
        }

        private void Combo_N_Load(object sender, EventArgs e)
        {
            lue_GoodCode.Properties.DataSource = my.bw.View_BlankCards_List(my.ACID);
            bindCarrier("");
            bindclass();
        }

        private void bindclass()
        {
            lue_Class.Properties.DataSource = my.bw.Combo_Class_Get("", my.ACID);
            lue_Class.Properties.DisplayMember = "ClassName";
            lue_Class.Properties.ValueMember = "ClassCode";

            lue_Class.EditValue = ClassCode;
        }

        private void bindCarrier(string carriername)
        {
            lue_Carrier.Properties.DataSource = my.bw.Carrier_Get("CarrierID", my.ACID);
            lue_Carrier.Text = carriername;
        }

        private void lookUpEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                Carrier_N frm = new Carrier_N();
                frm.FatherRefresh = bindCarrier;
                frm.ShowDialog();
            }
        }
    }
}